﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;
using System.Web.SessionState;

namespace wxx.ashx
{
    /// <summary>
    /// 生成代码时间： ljxtime
    /// 文件说明：t_lendsend
    /// 开发者：
    /// 最后编辑时间：
    /// 编辑说明：
    /// </summary>
    public class lendsend : baseashxClass
    {
        
        public override void ProcessRequest(HttpContext context)
        {
            wxx.BLL.t_lendsend bll = new wxx.BLL.t_lendsend(context); 
            base.ProcessRequest(context);
            context.Response.ContentType = "text/plain";

            mycommonClass mycommonClassobj = new mycommonClass();
            context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            if (context.Request.QueryString["type"] == "edit")//获取编辑信息
            {
                if (context.Request.QueryString["Id"] != null)
                {
                    string strret = bll.Geteditdata(context.Request.QueryString["Id"].ToString());//aa1
                    context.Response.Write(strret);
                }


            }
            else if (context.Request.QueryString["type"] == "del")//删除信息
            {
                if (context.Request.QueryString["Id"] == null)
                    return;
                string Id = context.Request.QueryString["Id"].ToString();                
                bll.Delete(int.Parse(Id));               
            }
            else if (context.Request.QueryString["type"] == "date")//获取应归还的日期
            { 
                string ReaderId = context.Request.QueryString["ReaderId"];
                wxx.BLL.t_readerinfo rdibll = new wxx.BLL.t_readerinfo();
                String ReaderType = rdibll.GetValue("ReaderType","ReaderId='"+ReaderId+"'");
                wxx.BLL.t_zidian zdbll = new wxx.BLL.t_zidian();
                string MaxDay = zdbll.GetValue("MaxDay","ReaderType='"+ReaderType+"'");
                DateTime NowTime = DateTime.Now;
                DateTime Send = NowTime.AddDays(int.Parse(MaxDay));
                context.Response.Write(Send);
            }
            else if (context.Request.QueryString["type"] == "combox")//保存修改或添加 信息 aa2
            {
                        if (context.Request.QueryString["comboxname"] == "ReaderId")
                        {
                            wxx.BLL.t_readerinfo bllzd = new wxx.BLL.t_readerinfo();
                            string strret = bllzd.GetListByColumn_tojson("Id,ReaderId,ReaderId,Name","1=1","");
                            context.Response.Write(strret);
                        }

                        if (context.Request.QueryString["comboxname"] == "BarCodeId")
                        {
                            wxx.BLL.t_librarybookinfo bllzd = new wxx.BLL.t_librarybookinfo();
                            string strret = bllzd.GetListByColumn_tojson("Id,BarCodeId,ISBN,BarCodeId","BookState="+ "'未借出'"+"or BookState="+"'已预约'","ISBN");
                            context.Response.Write(strret);
                        }


            }
            else if (context.Request.QueryString["type"] == "fine") {
                if (context.Request.QueryString["Id"] != null)
                {
                    wxx.BLL.t_librarybookinfo lbibll = new wxx.BLL.t_librarybookinfo();
                    wxx.Model.t_librarybookinfo lbimodel = new wxx.Model.t_librarybookinfo();
                    wxx.BLL.t_bookinfo bibll = new wxx.BLL.t_bookinfo();
                    wxx.Model.t_lendsend ldmodel = new wxx.Model.t_lendsend();
                    String Idld = context.Request.QueryString["Id"];

                    ldmodel = bll.GetModel(int.Parse(Idld));
                    string ReaderId = ldmodel.ReaderId;
                    string SendDate = ldmodel.SendDate.ToString();
                    string BarCodeId = ldmodel.BarCodeId.ToString();
                    string ShijiSendDate = DateTime.Now.ToString();
                    string BookState = context.Request.QueryString["BookState"];
                    TimeSpan ts;
                    String Fine = "";
                    String FineMoney = "0.00";
                    String ReaderType = "";
                    //判断罚款金额
                    if (BookState == "已归还")
                    {
                      

                        wxx.BLL.t_readerinfo rdibll = new BLL.t_readerinfo();
                        wxx.Model.t_readerinfo rdimodel = new Model.t_readerinfo();
                        DataSet rdids = rdibll.GetList("ReaderId='" + ReaderId + "'");
                        if (rdids.Tables[0].Rows.Count > 0)
                        { ReaderType = rdids.Tables[0].Rows[0][4].ToString(); }
                        //通过读者类型查看最大借阅数量
                        wxx.BLL.t_zidian zdbll = new BLL.t_zidian();
                        wxx.Model.t_zidian zdmodel = new Model.t_zidian();
                        DataSet zdds = zdbll.GetList("ReaderType='" + ReaderType + "'");
                        if (zdds.Tables[0].Rows.Count > 0)
                        {
                            Fine = zdds.Tables[0].Rows[0][4].ToString();
                        }
                        DateTime shijidt = DateTime.Parse(ShijiSendDate);
                        DateTime senddt = DateTime.Parse(SendDate);
                        if (DateTime.Compare(shijidt, senddt) > 0)
                        {
                            ts = shijidt.Subtract(senddt);
                            FineMoney = (ts.Days * double.Parse(Fine)).ToString();
                        }
                        
                    }
                    else
                    {
                        string ISBN1 = lbibll.GetValue("ISBN", "BarCodeId='" + BarCodeId + "'");
                        FineMoney = bibll.GetValue("JiaGe", "ISBN='" + ISBN1 + "'");

                    }
                    context.Response.Write(FineMoney);
                }
               

            }

            else if (context.Request.QueryString["type"] == "save")//保存修改或添加 
            {
                string ReaderId = context.Request.QueryString["ReaderId"];
                string BarCodeId = context.Request.QueryString["BarCodeId"];
                string LendDate1 = context.Request.QueryString["LendDate"];
                string LendDate = DateTime.Now.ToString();
                string SendDate = context.Request.QueryString["SendDate"];
                string ShijiSendDate = DateTime.Now.ToString();

                string FineMoney = context.Request.QueryString["FineMoney"];
                string Operation = context.Session["username"].ToString();
                string BookState = context.Request.QueryString["BookState"];
                String reason = context.Request.QueryString["reason"];
                wxx.BLL.t_librarybookinfo lbibll = new wxx.BLL.t_librarybookinfo();
                wxx.Model.t_librarybookinfo lbimodel = new wxx.Model.t_librarybookinfo();
                wxx.BLL.t_bookinfo bibll = new wxx.BLL.t_bookinfo();
                wxx.Model.t_bookinfo bimodel = new wxx.Model.t_bookinfo();
                wxx.Model.t_lendsend ldmodel = new wxx.Model.t_lendsend();
                wxx.BLL.t_order obll = new wxx.BLL.t_order();
                wxx.Model.t_order omodel = new wxx.Model.t_order();
                wxx.Model.t_lose lmodel = new wxx.Model.t_lose();
                wxx.BLL.t_lose lbll = new BLL.t_lose();
                DataSet lbids = new DataSet();

                if (context.Request.QueryString["Id"] != null && context.Request.QueryString["Id"] != "")//编辑
                {
                    String Idld = context.Request.QueryString["Id"];

                    ldmodel = bll.GetModel(int.Parse(Idld));
                    ReaderId = ldmodel.ReaderId;
                    BarCodeId = ldmodel.BarCodeId;
                    LendDate = ldmodel.LendDate.ToString();
                    SendDate = ldmodel.SendDate.ToString();
                   
                  
                    bll.Update(context.Request.QueryString["Id"].ToString(), ReaderId, BarCodeId, LendDate, SendDate, ShijiSendDate, FineMoney, Operation, BookState);
                    if (BookState == "已归还")//"已归还"是前面下拉框传进来的值，表示图书归还操作
                    {//图书的状态改为未借出,书目中数量增加一本，

                        lbids = lbibll.GetList("BarCodeId='" + BarCodeId + "'");
                        String ISBN = lbids.Tables[0].Rows[0][1].ToString();
                        String idlbi = lbids.Tables[0].Rows[0][0].ToString();

                        //借阅表中查询是否有人预定这本书,若有预约表中状态变成可借阅

                        DataSet ods = obll.GetList("ISBN='" + ISBN + "'" + "and State=" + "'已预约'");

                        if (ods.Tables[0].Rows.Count > 0)//有人预约
                        {
                            String Ido = ods.Tables[0].Rows[0][0].ToString();//默认早预约的
                            omodel = obll.GetModel(int.Parse(Ido));
                            omodel.State = "可借阅";
                            obll.Update1(omodel);

                            lbimodel = lbibll.GetModel(int.Parse(idlbi));
                            lbimodel.BookState = "已预约";
                            lbibll.Update1(lbimodel);//图书状态变成已预约
                            //库存数量增加
                            DataSet bids = bibll.GetList("ISBN='" + ISBN + "'");
                            String idbi = bids.Tables[0].Rows[0][0].ToString();
                            bimodel = bibll.GetModel(int.Parse(idbi));
                            String oldnum = bimodel.Beizhu;
                            bimodel.Beizhu = (int.Parse(oldnum) + 1).ToString();
                            bibll.Update1(bimodel);
                            context.Response.Write("yiorder");
                        }
                        else//没人预约
                        {

                            lbimodel = lbibll.GetModel(int.Parse(idlbi));
                            lbimodel.BookState = "未借出";
                            lbibll.Update1(lbimodel);
                            //库存数量增加
                            DataSet bids = bibll.GetList("ISBN='" + ISBN + "'");
                            String idbi = bids.Tables[0].Rows[0][0].ToString();
                            bimodel = bibll.GetModel(int.Parse(idbi));
                            String oldnum = bimodel.Beizhu;
                            bimodel.Beizhu = (int.Parse(oldnum) + 1).ToString();
                            bibll.Update1(bimodel);
                            context.Response.Write("true");
                        }


                    }//图书状态变成已遗失，遗失信息中新增一条记录
                    else
                    {

                        lbids = lbibll.GetList("BarCodeId='" + BarCodeId + "'");
                        String ISBN = lbids.Tables[0].Rows[0][1].ToString();
                        String idlbi = lbids.Tables[0].Rows[0][0].ToString();
                        lbimodel = lbibll.GetModel(int.Parse(idlbi));
                        lbimodel.BookState = "已遗失";
                        lbibll.Update1(lbimodel);


                        //DataSet bids = bibll.GetList("ISBN='" + ISBN + "'");
                        //String idbi = bids.Tables[0].Rows[0][0].ToString();
                        //bimodel = bibll.GetModel(int.Parse(idbi));
                        //String oldnum = bimodel.Beizhu;
                        //bimodel.Beizhu = (int.Parse(oldnum) - 1).ToString();
                        //bibll.Update1(bimodel);
                        //遗失信息中新增一条记录
                        wxx.BLL.t_lose lsbll = new wxx.BLL.t_lose();
                        wxx.Model.t_lose lsmodel = new wxx.Model.t_lose();
                        lsmodel.ReaderId = ReaderId;
                        lsmodel.LoseDate = DateTime.Now;
                        lsmodel.Reason = reason;
                        lsmodel.BarCodeId = BarCodeId;
                        lsmodel.Beizhu = "";
                        lsmodel.Operation = Operation;
                        lsbll.Add1(lsmodel);
                        context.Response.Write("true");
                    }

                }
                else//图书借阅处理
                {
                    //防止同一本书被同一个人借走，在借阅表中查看一下
                    DataSet ldds = bll.GetList("ReaderId='" + ReaderId + "'" + "and BarCodeId='" + BarCodeId + "'" + "and BookState=" + "'未归还'");
                    lbids = lbibll.GetList("BarcodeId='" + BarCodeId + "'");//通过BarCodeId找到这本书的ISBN
                    String ISBN1 = lbids.Tables[0].Rows[0][1].ToString();
                    String ISBN2 = "";
                    DataSet ldds1 = bll.GetList("ReaderId='" + ReaderId + "'" + "and BookState=" + "'未归还'");//获取这个人借的所有未归还的书

                    for (int i = 0; i < ldds1.Tables[0].Rows.Count; i++)
                    {
                        //通过BarCodeId去librarybookinfo查看ISBN若相同说明是同一本书
                        lbids = lbibll.GetList("BarcodeId='" + ldds1.Tables[0].Rows[i][2] + "'");
                        ISBN2 = lbids.Tables[0].Rows[0][1].ToString();
                        if (ISBN1 == ISBN2) break;
                    }
                    string flag = MaxNum1(ReaderId, context);

                    if (flag == "false") { context.Response.Write("chaochu"); }
                    else
                    {
                        if (ISBN1 == ISBN2) { context.Response.Write("chongfu"); }
                        else//通过BarCodeId查看图书的状态是不是已预约，如果是已预约，再检测一下借阅人是不是预约人
                        {
                            lbids = lbibll.GetList("BarcodeId='" + BarCodeId + "'");
                            if (lbids.Tables[0].Rows.Count > 0)
                            {
                                String lbiState = lbids.Tables[0].Rows[0][4].ToString();
                                String lbiISBN = lbids.Tables[0].Rows[0][1].ToString();
                                if (lbiState == "已预约")
                                {
                                    DataSet ods = obll.GetList("ReaderId='" + ReaderId + "'" + "and ISBN='" + lbiISBN + "'");

                                    if (ods.Tables[0].Rows.Count > 0) //说明预约人就是借阅人 // 
                                    {
                                        String Ido = ods.Tables[0].Rows[0][0].ToString();
                                        bll.Add(ReaderId, BarCodeId, LendDate, SendDate, ShijiSendDate, "0.00", Operation, "未归还");
                                        //将图书的状态变成已借阅

                                        lbids = lbibll.GetList("BarCodeId='" + BarCodeId + "'");
                                        String ISBN = lbids.Tables[0].Rows[0][1].ToString();
                                        String idlbi = lbids.Tables[0].Rows[0][0].ToString();
                                        lbimodel = lbibll.GetModel(int.Parse(idlbi));
                                        lbimodel.BookState = "已借出";
                                        lbibll.Update1(lbimodel);
                                        //书目信息中的数量相应减少

                                        DataSet bids = bibll.GetList("ISBN='" + ISBN + "'");
                                        String idbi = bids.Tables[0].Rows[0][0].ToString();
                                        bimodel = bibll.GetModel(int.Parse(idbi));
                                        String oldnum = bimodel.Beizhu;
                                        bimodel.Beizhu = (int.Parse(oldnum) - 1).ToString();
                                        bibll.Update1(bimodel);

                                        //预约表中状态变成已完成
                                        omodel = obll.GetModel(int.Parse(Ido));
                                        omodel.State = "已完成";
                                        obll.Update1(omodel);
                                        context.Response.Write("true");
                                    }
                                    else//说明借阅人不是预约人
                                    {

                                        context.Response.Write("notorder");
                                    }
                                }
                                else//说明没有被人预约可以直接借阅
                                {


                                    bll.Add(ReaderId, BarCodeId, LendDate, SendDate, ShijiSendDate, "0.00", Operation, "未归还");
                                    //将图书的状态变成已借阅

                                    lbids = lbibll.GetList("BarCodeId='" + BarCodeId + "'");
                                    String ISBN = lbids.Tables[0].Rows[0][1].ToString();
                                    String idlbi = lbids.Tables[0].Rows[0][0].ToString();
                                    lbimodel = lbibll.GetModel(int.Parse(idlbi));
                                    lbimodel.BookState = "已借出";
                                    lbibll.Update1(lbimodel);
                                    //书目信息中的数量相应减少

                                    DataSet bids = bibll.GetList("ISBN='" + ISBN + "'");
                                    String idbi = bids.Tables[0].Rows[0][0].ToString();
                                    bimodel = bibll.GetModel(int.Parse(idbi));
                                    String oldnum = bimodel.Beizhu;
                                    bimodel.Beizhu = (int.Parse(oldnum) - 1).ToString();
                                    bibll.Update1(bimodel);

                                    context.Response.Write("true");


                                }
                            }
                        }
                    }
                }

            }
            else if (Convert.ToString(context.Request.Form["action"]) == "query")
            {
                //string strret = bll.GetListByPageColumns_tojson("id,产品名称,产品数量,产品规格,备注", "1=1", "产品数量"); 加权限, 1=1 改为:部门编号 like '1010%'
                string strret = bll.GetListByPageColumns_tojson("Id,ReaderId,BarCodeId,LendDate,SendDate,ShijiSendDate,FineMoney,Operation,BookState", "BookState <>" + "'已归还'", "Id desc");
                context.Response.Write(strret);
                return;
            }
            else if (Convert.ToString(context.Request.Form["action"]) == "query1")
            {
                //string strret = bll.GetListByPageColumns_tojson("id,产品名称,产品数量,产品规格,备注", "1=1", "产品数量"); 加权限, 1=1 改为:部门编号 like '1010%'
                string strret = bll.GetListByPageColumns_tojson("Id,ReaderId,BarCodeId,LendDate,SendDate,ShijiSendDate,FineMoney,Operation,BookState", "BookState=" + "'未归还'", "Id desc");
                context.Response.Write(strret);
                return;
            }
            else if (Convert.ToString(context.Request.Form["action"]) == "query2")
            {
                //string strret = bll.GetListByPageColumns_tojson("id,产品名称,产品数量,产品规格,备注", "1=1", "产品数量"); 加权限, 1=1 改为:部门编号 like '1010%'
                string strret = bll.GetListByPageColumns_tojson("Id,ReaderId,BarCodeId,LendDate,SendDate,ShijiSendDate,FineMoney,Operation,BookState", "BookState=" + "'未归还'", "Id");
                context.Response.Write(strret);
                return;
            }
            else if (Convert.ToString(context.Request.Form["action"]) == "query3")
            {
                //string strret = bll.GetListByPageColumns_tojson("id,产品名称,产品数量,产品规格,备注", "1=1", "产品数量"); 加权限, 1=1 改为:部门编号 like '1010%'
                string strret = bll.GetListByPageColumns_tojson("Id,ReaderId,BarCodeId,LendDate,SendDate,ShijiSendDate,FineMoney,Operation,BookState", pagestrif, "Id desc");
                context.Response.Write(strret);
                return;
            }
            else if (context.Request.QueryString["type"] == "xujie")
            {
                String Id = context.Request.QueryString["Id"];
                wxx.BLL.t_lendsend ldbll = new BLL.t_lendsend();
                wxx.Model.t_lendsend ldmodel = new Model.t_lendsend();
                ldmodel = ldbll.GetModel(int.Parse(Id));
                DateTime sdt = DateTime.Parse(ldmodel.SendDate.ToString());
                ldmodel.SendDate = sdt.AddDays(30);
                ldmodel.BookState = "已续借";
                ldbll.Update1(ldmodel);
                context.Response.Write("true");
            }
            else
            {

            }
        }
        public string MaxNum1(string ReaderId,HttpContext context) {
            int Num = 0;
            int MaxNum = 0;
            string ReaderType = "";
            //查看借阅表中未归还状态是否超过
            wxx.BLL.t_lendsend ldbll = new BLL.t_lendsend();
            wxx.Model.t_lendsend ldmodel1 = new Model.t_lendsend();
            DataSet lsds = ldbll.GetList("ReaderId='" + ReaderId + "'" + "and BookState=" + "'未归还'");
            if (lsds.Tables[0].Rows.Count > 0)
                Num = int.Parse(lsds.Tables[0].Rows.Count.ToString());
            //通过读者id查看读者的类型
            wxx.BLL.t_readerinfo rdibll = new BLL.t_readerinfo();
            wxx.Model.t_readerinfo rdimodel = new Model.t_readerinfo();
            DataSet rdids = rdibll.GetList("ReaderId='" + ReaderId + "'");
            if (rdids.Tables[0].Rows.Count > 0)
                ReaderType = rdids.Tables[0].Rows[0][4].ToString();
            //通过读者类型查看最大借阅数量
            wxx.BLL.t_zidian zdbll = new BLL.t_zidian();
            wxx.Model.t_zidian zdmodel = new Model.t_zidian();
            DataSet zdds = zdbll.GetList("ReaderType='" + ReaderType + "'");
            if (zdds.Tables[0].Rows.Count > 0)
            {
                MaxNum = int.Parse(zdds.Tables[0].Rows[0][3].ToString());
            }
            if (MaxNum > Num)
                return "true";
            else
                return "false";
        }

    }
}